package com.sky.service.impl;
import java.math.BigDecimal;
import java.time.LocalDateTime;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Resource
    private DishMapper dishMapper;

    @Resource
    private SetMealMapper setMealMapper;

    @Override
    public Integer addCart(ShoppingCartDTO shoppingCartDTO) {

        Integer tag = 0;
        // 配置userid
        shoppingCartDTO.setUserId(BaseContext.getCurrentId());
        // 添加购物车 11：09 - 11：47 = 28min
        // 1. 去购物车查询历史内容
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByList(shoppingCartDTO);

        // 2.判断购物车是否有内容
        if (shoppingCartList == null || shoppingCartList.size() == 0) {
            // 2.1 没有菜品，插入数据
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setUserId(shoppingCartDTO.getUserId());
            shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            // 判断当前为菜品
            if (shoppingCartDTO.getDishId() != null) {
                shoppingCart.setDishId(shoppingCartDTO.getDishId());
                Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
                // 冗余字段
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            }else  {
                // 当前为套餐
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
                Setmeal setmeal = setMealMapper.selectById(shoppingCartDTO.getSetmealId());
                // 冗余字段
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }

            log.info("【新增购物车前的数据】cart:{}",shoppingCart);
            tag = shoppingCartMapper.insert(shoppingCart);

        } else  {
            // 3. 有内容更新
            ShoppingCart shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            log.info("【更新购物车数量前的数据】cart:{}",shoppingCart);
            tag = shoppingCartMapper.update(shoppingCart);

        }
        return tag;




        // 1. 插入购物车
    }

    @Override
    public List<ShoppingCart> findAll() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.selectByUserId(userId);
    }

    @Override
    public Integer clean() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.deleteByUserId(userId);
    }
}
